<template>
  <div class="access-page">
    <h4 class="zl-title">访问权列表<Button type="primary" @click="accessAddModify()" class="fr"><Icon type="md-add" /> 添加访问权</Button></h4>
    <div class="zl-page-loading" v-if="isLoading">
      <Spin fix></Spin>
    </div>
    <Table :columns="columns" :data="accessList" class="access-table"></Table>

    <!-- 新增、修改 -->
    <add-modify-model :visible.sync="accessVisible" :form="accessForm" @finish="init" ></add-modify-model>
    <!-- 详情 -->
    <detail-model :visible.sync="detailVisible" :form="accessForm"></detail-model>
  </div>
</template>
<script type="text/javascript">
import Fetch from '@/api/request'
import AddModifyModel from '@/components/access-right/add-modify-model'
import DetailModel from '@/components/access-right/detail-model'
export default {
  components: { AddModifyModel, DetailModel },
  data() {
    return {
      columns: [{
        type: 'index',
        width: 60
      }, {
        title: '名称',
        key: 'id'
      }, {
        title: '域名',
        render: (h, params) => {
          return h('a', {
            attrs: {
              'target': '_blank',
              'href': 'http://' + params.row.host
            }
          }, params.row.host)
        }
      }, {
        title: '创建时间',
        key: 'creationTimestamp'
      }, {
        title: '操作',
        render: (h, params) => {
          return h('div', [
            h('div', {
              class: 'button-inner',
              on: {
                click: () => {
                  this.accessDetail(params.row)
                }
              }
            }, [
              h('Icon', {
                props: {
                  type: 'ios-paper'
                }
              }, ''),
              '详情'
            ]),
            h('div', {
              class: 'button-inner',
              on: {
                click: () => {
                  this.accessAddModify(params.row)
                }
              }
            }, [
              h('Icon', {
                props: {
                  type: 'ios-create-outline'
                }
              }, ''),
              '编辑'
            ]),
            h('div', {
              class: 'button-inner',
              on: {
                click: () => {
                  this.accessDelete(params.row)
                }
              }
            }, [
              h('Icon', {
                props: {
                  type: 'md-trash'
                }
              }, ''),
              '删除'
            ])
          ])
        }
      }],
      accessList: [],
      accessForm: {},
      accessVisible: false,
      detailVisible: false,
      isLoading: true
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    // 删除访问权
    accessDelete(access) {
      this.$Modal.confirm({
        title: '系统提示',
        content: '确定删除访问权（' + access.id + '）?',
        loading: true,
        onOk: () => {
          Fetch.delete({
            url: '/api/environments/' + this.$route.params.environmentId + '/ingresses/' + access.id
          }).then((res) => {
            if (res && res.code === '0') {
              this.$Message.success({
                content: '删除访问权（' + access.id + '）成功！',
                duration: this.DURATION
              })
              this.init()
            } else {
              this.$Message.warning({
                content: res.message || '服务器内部错误',
                duration: this.DURATION
              })
            }
            this.$Modal.remove()
          }).catch((err) => {
            this.$Modal.remove()
            this.$Message.error(err.message)
          })
        }
      })
    },
    // 访问权详情
    accessDetail(access) {
      if (access) {
        let d = access.host.split('.')
        let t = ''
        if (d.length > 2) {
          for (let i = 0; i < d.length - 2; i++) {
            const item = d[i]
            if (i === 0) {
              t += item
            } else {
              t += '.' + item
            }
          }
        }
        this.accessForm = Object.assign({environmentName: access.environmentId, level3domen: t}, access)
      }
      this.detailVisible = true
    },
    // 访问权新增、修改
    accessAddModify(access) {
      if (access) {
        let d = access.host.split('.')
        let t = ''
        if (d.length > 2) {
          for (let i = 0; i < d.length - 2; i++) {
            const item = d[i]
            if (i === 0) {
              t += item
            } else {
              t += '.' + item
            }
          }
        }
        this.accessForm = Object.assign({environmentName: access.environmentId, level3domen: t}, access)
      } else {
        this.accessForm = {
          environmentName: '',
          id: '',
          host: localStorage.getItem('domain'),
          maxBodySize: '',
          level3domen: '',
          paths: []
        }
      }
      this.accessVisible = true
    },
    // 获取访问权初始数据
    init() {
      Fetch.get({
        url: '/api/environments/' + this.$route.params.environmentId + '/ingresses'
      }).then((res) => {
        if (res && res.code === '0') {
          this.accessList = res.data
        } else {
          this.$Message.warning({
            content: res.message || '服务器内部错误',
            duration: this.DURATION
          })
        }
        this.isLoading = false
      }).catch((err) => {
        this.isLoading = false
        this.$Message.error(err.message)
      })
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.access-page {
  padding: 20px;
  .access-table {
    margin-top: 20px;
  }
  .zl-title {
    margin-top: 10px;
  }
}
</style>
